import { _decorator, AudioClip, AudioSource, Component, director, Node } from 'cc';
import { Res } from './Res';
import { ld } from './LocalData';
const { ccclass, property } = _decorator;

export class Audioer {
    /**音乐 */
    private audio_music: AudioSource = null;
    /**音效 */
    private audio_effect: AudioSource = null;

    private _volume_music: number = 1;
    /**音乐音量 */
    public get volume_music(): number { return this._volume_music };
    public set volume_music(volume: number) {
        this._volume_music = volume;
        this.audio_music.volume = volume;
        if(volume > 0 && !this.audio_music.playing && this.curr_playing){
            this.audio_music.play();
        }
    }
    private _volume_effect: number = 1;
    /**音效音量  */
    public get volume_effect(): number { return this._volume_effect };
    public set volume_effect(value: number) { this._volume_effect = value };

    /**记录当前播放的音乐名 */
    private curr_playing = null;

    /** 初始化 */
    public static init(node: Node){ audioer.init(node) };
    private init(node: Node){
        this.audio_music = node.addComponent(AudioSource);
        this.audio_music.playOnAwake = false;
        this.audio_effect = node.addComponent(AudioSource);
        this.audio_effect.playOnAwake = false;
        this.loadVolume();
    }


    /**
     * 播放音乐
     * @param key 路径键值
     * @param loop 是否循环播放，默认循环
     * @returns 
     */
    public playMusic(key: string, loop: boolean = true) {
        if(key == this.curr_playing) return;//在播放中直接返回
        
        Res.get(key, (clip: AudioClip) =>{
            if(this.curr_playing) this.audio_music.stop(); //之前有播放则停止
            this.audio_music.loop = loop;
            this.audio_music.clip = clip;
            if(this.volume_music > 0) this.audio_music.play();//音量大于0才播放
            this.curr_playing = key;
        });
    }

    stopMusic() {
        this.curr_playing = null;
        this.audio_music.stop();
    }

    /** 播放音效*/
    playEffect(key: string) {
        if (this.volume_effect == 0) return;
        Res.get(key, (clip: AudioClip) =>{
            this.audio_effect.playOneShot(clip, this.volume_effect);
        });
    }

    /** 播放音效*/
    playEffect2(key: string, volume: number) {
        if (this.volume_effect == 0) return;
        Res.get(key, (clip: AudioClip) =>{
            this.audio_effect.playOneShot(clip, volume * this.volume_effect);
        });
    }

    /**将调整的音量保存 */
    public saveVolume() {
        localStorage.setItem(ld.TAG + "_VOLUMES", JSON.stringify({
            music: this.volume_music,
            effect: this.volume_effect
        }));
    }
    /**从本地加载上次保存的音量 */
    public loadVolume() {
        let vols = localStorage.getItem(ld.TAG + "_VOLUMES");
        let data: { music: number, effect: number };
        if (!vols) data = { music: 1, effect: 1 };
        else data = JSON.parse(vols);

        this.volume_music = data.music;
        this.volume_effect = data.effect;
    }

}

export const audioer = new Audioer();